package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
public class SetMealController {

    @Autowired
    public SetmealService setmealService;

    @Autowired
    public CategoryService categoryService;

    @Autowired
    public SetmealDishService setmealDishService;

    @Autowired
    public DishService dishService;

    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){

        setmealService.saveWithDish(setmealDto);

        return R.success("dm");

    }

    @GetMapping("/page")
    public R<Page> getdm(int page,int pageSize,String name){

        Page<Setmeal> page1 = new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);
        wrapper.orderByDesc(Setmeal::getUpdateTime);

        setmealService.page(page1,wrapper);
        List<Setmeal> setmeals = page1.getRecords();
        ArrayList<SetmealDto> list = new ArrayList<>();
        Page<SetmealDto> page2 = new Page<>();

        for (Setmeal setmeal : setmeals) {

            SetmealDto setmealDto = new SetmealDto();

            BeanUtils.copyProperties(setmeal,setmealDto);

            Long categoryId = setmeal.getCategoryId();

            Category byId = categoryService.getById(categoryId);

            setmealDto.setCategoryName(byId.getName());

            list.add(setmealDto);
        }

        page2.setRecords(list);
        page2.setTotal(page1.getTotal());
        return R.success(page2);

    }

@DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){

        setmealService.rmoveWithDish(ids);
        return R.success("删除成功");

}
    /**
     * 根据条件查询套餐数据
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }
    @PostMapping("/status/{st}")
    public R<String> destatus(@PathVariable int st,@RequestParam List<Long> ids){
        List<Setmeal> setmeals = ids.stream().map((item) -> {
            Setmeal setmeal = new Setmeal();
            setmeal.setId(item);
            setmeal.setStatus(st);
            return setmeal;

        }).collect(Collectors.toList());

        setmealService.updateBatchById(setmeals);

        return R.success("修改成功");

    }

    //修改回显
    @GetMapping("/{id}")
    public R<SetmealDto> getdto(@PathVariable Long id) {

        SetmealDto setmealDto=setmealService.getByIdwithDish(id);

        return R.success(setmealDto);
    }

    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto) {

        setmealService.updatwithDish(setmealDto);
        return R.success("修改成功");
    }

    @GetMapping("/dish/{id}")
    public R<List<SetmealDish>> lists(@PathVariable Long id){
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(wrapper);
//        Long dishId = setmealDishService.getById(wrapper).getDishId();
//        LambdaQueryWrapper<Dish> wrapper1 = new LambdaQueryWrapper<>();
//        wrapper1.eq(Dish::getId,dishId);
//        List<Dish> list = dishService.list(wrapper1);
        return R.success(list);

    }


}
